package ca.camosun.inventory.gui;

import java.util.List;

import android.app.ActionBar;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.Toast;
import ca.camosun.inventory.Box;
import ca.camosun.inventory.Box.ItemList;
import ca.camosun.inventory.Item;
import ca.camosun.inventory.R;

public class ListItemsInOneBoxActivity extends Activity {
	private ListView listName;
	private ArrayAdapter<String> listNameAdapter;
	private ListView listQuantity;
	private ArrayAdapter<String> listQuantityAdapter;
	private String boxName;
	private Box aBox;

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.items_list_view);
		
		ActionBar actionBar = getActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);

		boxName = getIntent().getExtras().getString("boxName");
		aBox = new Box(boxName, this);

		listName = (ListView) findViewById(R.id.listName);
		listNameAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, aBox.getItemNames());
		listName.setAdapter(listNameAdapter);

		listQuantity = (ListView) findViewById(R.id.listQuantity);
		listQuantityAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, aBox.getItemQuantities());
		listQuantity.setAdapter(listQuantityAdapter);

		listName.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				OnClickListItem(position);

			}
		});

		listQuantity.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				OnClickListItem(position);

			}
		});

		registerForContextMenu(listName);

	}

	@Override
	public void onResume() {
		super.onResume();
		aBox = new Box(boxName, this);
		runOnUiThread(refreshView);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	public void createItem(View view) {

		EditText editItemName = (EditText) findViewById(R.id.editItemName);
		String itemName = editItemName.getText().toString();
		
		Item newItem = new Item(itemName, itemName, 1);
		ItemList status = aBox.addItem(newItem, this);
		Toast.makeText(this, status.toString(), Toast.LENGTH_SHORT).show();
		
		runOnUiThread(refreshView);
		editItemName.setText("");

	}

	public void OnClickListItem(int position) {

		List<Item> allItems = aBox.getItems(this);
		String itemName = (String) allItems.get(position).getName();

		Intent toViewItem = new Intent(this, ViewOneItem.class);
		toViewItem.putExtra("boxName", boxName);
		toViewItem.putExtra("itemName", itemName);

		startActivity(toViewItem);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);

		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
		String selectedItem = ((TextView) info.targetView).getText().toString();

		menu.setHeaderTitle(selectedItem);

		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.activity_box, menu);

	}

	@Override
	public boolean onContextItemSelected(MenuItem menuItem) {
		if (menuItem.getItemId() == R.id.delete_item_button) {
			AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuItem
					.getMenuInfo();
			List<Item> allItems = aBox.getItems(this);
			String itemName = (String) allItems.get(info.position).getName();
			Item fakeItem = new Item(itemName, itemName, 1);
			aBox.removeItem(fakeItem, this);

			runOnUiThread(refreshView);
			return true;
		}

		return false;

	}

	Runnable refreshView = new Runnable() {
		public void run() {
			listNameAdapter.clear();
			listNameAdapter.addAll(aBox.getItemNames());
			listNameAdapter.notifyDataSetChanged();

			listQuantityAdapter.clear();
			listQuantityAdapter.addAll(aBox.getItemQuantities());
			listQuantityAdapter.notifyDataSetChanged();

		}

	};
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.action_bar_menu, menu);
		
		return true;
	}

}
